
import { DmgTypeKey, Enumerate, IntervalTimeUnit, ONCE_DMG_DTL, Status, UnitAttrStruct } from "../../types/L_Type"
import UnitX from "../../entity/UnitX"
import DmgExecutor from "../../module/dmgabout/DmgExecutor"
import DmgFormula from "../../module/dmgabout/DmgFormula"
import L_Run from "../../utils/L_Run"
import EventX from "./EventX"

/**是否使用简化初始化需要的参数 */
export type DOT_ONCE_DMG_DTL=({autoExecDmgEff:true,additTrgProb:Enumerate<101>,aux:UnitX,bux:UnitX,extraDmgAttr?:UnitAttrStruct} & ONCE_DMG_DTL ) 
                            | ({autoExecDmgEff:false,callback:Function,aux:UnitX,bux:UnitX,extraDmgAttr?:UnitAttrStruct} &  Pick<ONCE_DMG_DTL,'oriDmg' | 'dmgTypeKey' >  ) //Record<'autoExecDmgEff':boolean, Partial<Pick<ONCE_DMG_DTL,'effectPath' | 'attPoint'>> & Pick<ONCE_DMG_DTL,'oriDmg' | 'dotDmgTypeKey'>>



/** dot事件 */
export default class DotEvent extends EventX {
    
    dtl:DOT_ONCE_DMG_DTL
    private _bux_status?:Status


    constructor(
        intervalUnit: IntervalTimeUnit,
        intervalVal: number,
        execLastedNums: number,
        dtl:DOT_ONCE_DMG_DTL,
    ) {
        super('DOT',intervalUnit,intervalVal,execLastedNums)
        this.dtl=dtl
        this._bux_status=this.getStatusType(dtl.dmgTypeKey)
    } 



    public beAddedAndExec(): void {
        //!!!注意dot类伤害不一样 只在第一次判定是否命中  后续使用dfhit必中攻击来执行
        let hit=DmgFormula.hitCpx(this.dtl.aux,this.dtl.bux,false,this.dtl.dmgTypeKey,this.dtl.extraDmgAttr)  
        if(hit){
            super.beAddedAndExec()
            console.log("...dot 被添加到队列执行...")
            if(this._bux_status!=undefined){
                this.dtl.bux.statusAddX(this._bux_status)
            }
        }else{
            console.log("dot未命中!!")
        }
    }
    public start(): void {
        //DOT注册到单位状态事件中
        this.dtl.bux.eventTypeMapAddx(this.eventType,this)
        //this.dtl.bux.getEventTypeMap(this.eventType).set(this.eventId,this)
        console.log("...dot start...")
    }
    public run(): void {
        //console.log("...dot run...")
        
        //简化执行 或者自定义回调执行
        if(this.dtl.autoExecDmgEff){
            let frs=DmgExecutor.dmgUnitByAbilityDfHit(this.dtl.aux,this.dtl.bux,this.dtl.oriDmg,this.dtl.additTrgProb,this.dtl.dmgTypeKey,this.dtl.extraDmgAttr)
            if(frs.hit){
                L_Run.CreateEffectOnce(this.dtl.bux.unit,this.dtl.effectPath,this.dtl.effAttPoint)
            }
        }else{
            this.dtl.callback()
        }
    }

    public end(): void {
        this.dtl.bux.eventTypeMapRemovex(this.eventType,this)
        //this.dtl.bux.getEventTypeMap(this.eventType).delete(this.eventId)
        if(this._bux_status!=undefined){
            this.dtl.bux.statusRemoveX(this._bux_status)
        }
        console.log("...dot end...")
    }


    /**通过dot伤害类型推断出来的单位附加状态 */
    private getStatusType(dmgTypeKey:DmgTypeKey):Status | undefined{
        if(dmgTypeKey=='poison'){
            return 'Poisoned'
        }else if(dmgTypeKey=='fire'){
            return 'Burning'
        }else if(dmgTypeKey=='ice'){
            return 'Frosting'
        }
        return undefined

    }

}  